﻿using Scheduler;

using System;
using System.Collections.Generic;
using System.Threading;
using Engine.Result;
using EventArgs = Scheduler.EventArgs;
using System.Diagnostics;

namespace Engine.Middlewares
{
    public class VirtualAutoOpticalInspectionMiddleware : Middleware
    {
        public VirtualAutoOpticalInspectionMiddleware(string str)
        {
            Console.WriteLine($"设备ID:{str}");
        }
        
        public override bool Filter(Context ctx, EventArgs args)
        {
            return true;
        }

        protected override void DoHandleEvent(Context ctx, EventArgs args)
        {
            switch (args.Id)
            {
                case EventId.Init:
                    //TODO 
                    break;
                case EventId.Cleanup:
                    break;
                case EventId.PassBegin:
                    break;
                case EventId.PassEnd:
                    break;
                case EventId.Handle:
                    HandleItem(ctx as WaferTesterContext, args);
                    break;
                case EventId.UserDefinedEventId:
                    break;
                default:
                    break;
            }

        }

        private void HandleItem(WaferTesterContext ctx, Scheduler.EventArgs userData)
        {
            if (userData.UserData[2] is ChipResult cr)
            {
                var DATA = ctx.Result.GetChipResultById((cr.Id));

                DATA._itemResults.Add(new List<ItemResult>() { new ItemResult() { Aoi = true, Flags = ItemResultFlags.Ok } });

                //DATA._itemResults[(int)userData.UserData[1]].AddRange(new ItemResult[5]);


            }

          Thread.Sleep(5000); 

        }
    }
}